package laboratoryQ.flatness;

import java.awt.Graphics;
import java.awt.Rectangle;

public class BoardTransformer implements ICoordinateTransformer {
	private double boardSize;
	private RangeInfo rangeInfo;
	private boolean commonScaleMult;

	public BoardTransformer(double boardSize, RangeInfo rangeInfo, boolean commonScaleMult) {
		this.boardSize = boardSize;
		this.rangeInfo = rangeInfo;
		this.commonScaleMult = commonScaleMult;
	}

	@Override
	public void transform(
			double[] srcX, 
			double[] srcY, 
			Graphics g
	) {
		Rectangle r = g.getClipBounds();
		double rX = r.width - boardSize * 2;
		double rY = r.height - boardSize * 2;
		
		double mulX = getMul(rangeInfo.minX, rangeInfo.maxX, rX);
		double mulY = getMul(rangeInfo.minY, rangeInfo.maxY, rY);
		if (commonScaleMult) {
			double mul = Math.min(mulX, mulY);
			mulX = mul;
			mulY = mul;
		}
		
		int n = srcX.length;
		for (int i = 0; i < n ; i++) {
			srcX[i] = getTransf(srcX[i], rangeInfo.minX, boardSize, mulX);
			srcY[i] = getTransf(srcY[i], rangeInfo.minY, boardSize, mulY);
		}
	}

	private double getTransf(
			double from, 
			double minFrom, 
			double minTo,
			double mul
	) {
		return (from - minFrom) * mul + minTo;
	}

	private double getMul(double min, double max, double range) {
		return range / (max - min);
	}
}
